package com.hyb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyb.Vo.DictionaryVo;
import com.hyb.entity.Dictionary;
import com.hyb.entity.Product;
import com.hyb.mapper.DictionaryMapper;
import com.hyb.page.PageReq;
import com.hyb.page.VoQuery;
import com.hyb.service.DictionaryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  数据字典Service接口实现类
 * </p>
 *
 * @author hyb
 * @since 2021-12-29
 */
@Service
public class DictionaryServiceImpl extends ServiceImpl<DictionaryMapper, Dictionary> implements DictionaryService {
    @Autowired
    private DictionaryMapper dictionaryMapper;

    @Override
    public List<Dictionary> getStaffStates() {
        QueryWrapper<Dictionary> dictionaryQueryWrapper = new QueryWrapper<>();
        dictionaryQueryWrapper.eq("parent_id", 4);

        return dictionaryMapper.selectList(dictionaryQueryWrapper);
    }

    @Override
    public PageReq loadAllDictionary(Integer limit, Integer current,Integer parentId,String dictionaryValue,String dictionaryEntry) {
        PageReq pageReq = new PageReq();
        Page<Dictionary> page = new Page<>(current,limit);
        VoQuery voQuery = new VoQuery();
        if (parentId>0) {
            voQuery.setDictionariesType(parentId);
        }
        if (!StringUtils.isEmpty(dictionaryEntry)) {
            voQuery.setDictionariesEntry(dictionaryEntry);
        }
        if (!StringUtils.isEmpty(dictionaryValue)) {
            voQuery.setDictionariesValue(dictionaryValue);
        }
        IPage<Dictionary> allDictionaries = dictionaryMapper.getAllDictionaries(page, voQuery);
        Map<String,Object> map = new HashMap<>();
        List<Dictionary> records = allDictionaries.getRecords();
        List<DictionaryVo> list = new ArrayList<>();
        for (Dictionary record : records) {
            DictionaryVo dictionaryVo = new DictionaryVo();
            dictionaryVo.setDictionaryId(record.getDictionaryId());
            dictionaryVo.setDictionaryEntry(record.getDictionaryEntry());
            dictionaryVo.setDictionaryValue(record.getDictionaryValue());
            dictionaryVo.setIsEdit(record.getIsEdit());
            dictionaryVo.setParentId(record.getParentId());
            Dictionary dictionary = dictionaryMapper.selectById(record.getParentId());
            dictionaryVo.setDictionaryType(dictionary.getDictionaryValue());
            list.add(dictionaryVo);
        }
        map.put("dictionaries",list);
        pageReq.setTotal(allDictionaries.getTotal());
        pageReq.setMyData(map);
        return pageReq;
    }

    @Override
    public List<Dictionary> loadDictionaryType() {
        QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("dictionary_id","dictionary_value");
        queryWrapper.eq("parent_id",1);
        return dictionaryMapper.selectList(queryWrapper);
    }

    @Override
    public void saveDictionary(Dictionary dictionary) {
        dictionaryMapper.insert(dictionary);
    }

    @Override
    public Dictionary getDictionaryByDictionaryId(Integer dictionaryId) {
        return dictionaryMapper.selectById(dictionaryId);
    }

    @Override
    public void updateDictionary(Dictionary dictionary) {
        QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dictionary_id",dictionary.getDictionaryId());
        dictionaryMapper.update(dictionary,queryWrapper);
    }

    @Override
    public void deleteDictionary(Integer dictionaryId) {
        dictionaryMapper.deleteById(dictionaryId);
    }

    @Override
    public List<Dictionary> getProductType() {
        QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("dictionary_entry");
        queryWrapper.eq("parent_id",3);
        return dictionaryMapper.selectList(queryWrapper);
    }

    @Override
    public List<Dictionary> getSaleType() {
        QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",55);
        queryWrapper.ne("dictionary_value","开发失败");
        queryWrapper.ne("dictionary_value","已完成");
        queryWrapper.select("dictionary_value","dictionary_entry");
        return dictionaryMapper.selectList(queryWrapper);
    }

    @Override
    public List<Dictionary> getCustomerGrade() {
        QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",2);
        queryWrapper.select("dictionary_value","dictionary_entry");
        return dictionaryMapper.selectList(queryWrapper);
    }

    @Override
    public List<Dictionary> getCustomerSatisfaction() {
        QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<Dictionary>();
        queryWrapper.eq("parent_id",5);
        queryWrapper.select("dictionary_value","dictionary_entry");
        return dictionaryMapper.selectList(queryWrapper);
    }

    @Override
    public List<Dictionary> getCustomerCredit() {
        QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",6);
        queryWrapper.select("dictionary_value","dictionary_entry");
        return dictionaryMapper.selectList(queryWrapper);
    }

    @Override
    public List<Dictionary> getOrderTypes() {
        QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",58);
        queryWrapper.select("dictionary_value","dictionary_entry");
        return dictionaryMapper.selectList(queryWrapper);
    }

    @Override
    public List<Dictionary> getCustomerStates() {
        List<Dictionary> customerStates = dictionaryMapper.selectCustomerStates();
        return customerStates;
    }

}
